package test.hxy.mongoSchoolSimple.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.hxy.mongoSchoolSimple.dao.BookDao;
import test.hxy.mongoSchoolSimple.dao.BookTemplateDao;
import test.hxy.mongoSchoolSimple.model.Book;

import java.util.List;

@Service
public class BookService {
    @Autowired
    BookDao bookDao;
    @Autowired
    BookTemplateDao bookTemplateDao;
    public List<Book> findAll(){
        return bookDao.findAll();
    }

    public List<Book> findByBookNameContains(String bookName) {
        return bookDao.findByBookNameContains(bookName);
    }

    public Book findByisbnEquals(String isbn) {
        return bookDao.findByisbnEquals(isbn);
    }

    public List<Book> findByteacherIdEquals(String teacherId) {
        return bookTemplateDao.findByteacherIdEquals(teacherId);
    }

    public List<Book> findByPublisherEquals(String publisher) {
        return bookDao.findByPublisherEquals(publisher);
    }
    public Book insertBook(Book book) {
        book.set_id(book.getIsbn());
        Book temp = bookDao.findByisbnEquals(book.getIsbn());
        if (temp == null) {
            bookTemplateDao.insert(book);

            return book;
        } else {
            return null;
        }
}
    public Book delete(String isbn) {
        Book temp = bookDao.findByisbnEquals(isbn);
        if (temp != null) {
            bookTemplateDao.delete(isbn);
            return temp;
        } else {
            return null;
        }
    }
}
